rimer = function(result, X, Z, Label='') {
  if(str_detect(string=result,
                pattern='tab2')) {
    if.Y = 'Q1'
    if.data = dv_survey
    if(Z=='Prop.Citizen') {
      TreatedHistogram = subset(citizen.survey.histogram, 
                                fill=='black')
      ControlHistogram = subset(citizen.survey.histogram, 
                                fill=='white')
    } else {
      TreatedHistogram = subset(firm.survey.histogram, 
                                fill=='black')
      ControlHistogram = subset(firm.survey.histogram, 
                                fill=='white')
    }      
  } else {
    if.Y = 'Spoke'
    if(result=='tab3col3') {
      if.data = dv_pooled
      if(Z=='Prop.Citizen') {
        TreatedHistogram = subset(citizen.pooled.histogram, 
                                  fill=='black')
        ControlHistogram = subset(citizen.pooled.histogram, 
                                  fill=='white')
      } else {
        TreatedHistogram = subset(firm.pooled.histogram, 
                                  fill=='black')
        ControlHistogram = subset(firm.pooled.histogram, 
                                  fill=='white')
      }      
    } else if(result=='tab3col4') {
      if.data = dv_floor
      if(Z=='Prop.Citizen') {
        TreatedHistogram = subset(citizen.floor.histogram, 
                                  fill=='black')
        ControlHistogram = subset(citizen.floor.histogram, 
                                  fill=='white')
      } else {
        TreatedHistogram = subset(firm.floor.histogram, 
                                  fill=='black')
        ControlHistogram = subset(firm.floor.histogram, 
                                  fill=='white')
      }      
    } else {
      if.data = dv_caucus
      if(Z=='Prop.Citizen') {
        TreatedHistogram = subset(citizen.caucus.histogram, 
                                  fill=='black')
        ControlHistogram = subset(citizen.caucus.histogram, 
                                  fill=='white')
      } else {
        TreatedHistogram = subset(firm.caucus.histogram, 
                                  fill=='black')
        ControlHistogram = subset(firm.caucus.histogram, 
                                  fill=='white')
      }      
    }
  }
  if(Z=='Prop.Citizen') {
    if.Z = 'FirmXProp.Firm'
    Zrange = seq(from=0, 
                 to=0.71, 
                 by=0.01)
    xAxisLabel = 'Moderator: % citizen'
    yAxisLabel = 'Spillover effect of citizen treatment'
  } else if(Z=='Prop.Firm') {
    if.Z = 'CitizenXProp.Citizen'
    Zrange = seq(from=0, 
                 to=0.88, 
                 by=0.01)
    xAxisLabel = 'Moderator: % firm'
    yAxisLabel = 'Spillover effect of firm treatment'
  }
  me.sigs = NULL
  ri.ests = subset(permutation.results, 
                   Result==result & term==Z)$estimate %*% t(Zrange)
  our.est = subset(experimental.results, 
                   Result==result & term==Z)$estimate*Zrange
  for(z in 1:length(Zrange)) {
    qest = ecdf(ri.ests[,z])
    me.sigs[z] = qest(our.est[z]) %>%
      multiply_by(1e3) %>%
      round %>%
      as.integer %>%
      mapvalues(from=seq(0L, 1000L, 1L),
                to=c(rep('p \u2264 0.01', 6), 
                     rep('p \u2264 0.05', 20), 
                     rep('p > 0.05', 950), 
                     rep('p \u2264 0.05', 20), 
                     rep('p \u2264 0.01', 5)),
                warn_missing=F)
  }
  me.sigs = factor(x=me.sigs, 
                   levels=c('p > 0.05',
                            'p \u2264 0.05',
                            'p \u2264 0.01'))
  Labels = list(expression(p > 0.05), 
                expression(p <= 0.05), 
                expression(p <= 0.01))
  if.D = X
  if.X = Z
  if.Z = c(if.Z, 
           setdiff(subset(experimental.results, 
                          Result==result)$term, 
                   c(if.D, if.X, '(Intercept)','Citizen:Prop.Citizen','Firm:Prop.Firm')))
  kern.est = inter.kernel.spillover(Y=if.Y, 
                                    D=if.D, 
                                    X=if.X, 
                                    Z=if.Z, 
                                    data=if.data, 
                                    na.rm=T, 
                                    CI=F, 
                                    cores=6)$est[,c('X','x')] %>%
    set_colnames(c('Z','dYdX')) %>%
    mutate(dYdX=Z*dYdX)
  ri.ests = as.data.frame(ri.ests) %>%
    mutate(ID=1:1e4) %>%
    gather(key='Z', value='dYdX', -ID) %>% 
    mutate(Z=mapvalues(x=Z, 
                       from=paste0('V', 1:length(Zrange)), 
                       to=Zrange),
           Z=as.numeric(Z))
  data.frame(Significance=me.sigs, 
             Z=Zrange, 
             dYdX=our.est) %>%
    ggplot(aes(x=Z, y=dYdX, fill=Significance)) +
    geom_line(data=ri.ests, 
              aes(x=Z, y=dYdX, group=ID), 
              color='black', alpha=0.01, inherit.aes=F) +
    geom_line(data=kern.est, 
              aes(x=Z, y=dYdX), 
              color='#e63e40', linetype=1, alpha=1, size=0.5, inherit.aes=F) + 
    geom_hline(yintercept=0, linetype=2, color='white') +
    geom_point(size=2, shape=21) +
    geom_rect(data=TreatedHistogram, 
              aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), 
              fill='black', color='black', size=0.25, inherit.aes=F) +
    geom_rect(data=ControlHistogram, 
              aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), 
              fill='white', color='black', size=0.25, inherit.aes=F) +
    geom_text(data=data.frame(x=0.05, y=0.875, label=Label, stringsAsFactors=F),
              aes(x=x, y=y, label=label),
              inherit.aes=F, hjust=0, vjust=0.5, size=5) +
    scale_x_continuous(breaks=seq(from=0, 
                                  to=1, 
                                  by=0.2), 
                       labels=paste0(str_pad(string=seq(from=0, 
                                                        to=100, 
                                                        by=20), 
                                             width=3, 
                                             side='left', 
                                             pad=' '), 
                                     '%')) +
    scale_fill_brewer(labels=Labels, type='seq', palette='YlGnBu', direction=1, drop=F) +
    guides(fill=guide_legend(override.aes=list(size=2))) +
    labs(x=xAxisLabel, y=yAxisLabel) +
    coord_fixed(ratio=0.25, xlim=c(0,1), ylim=c(-1,1), expand=F) +
    theme_minimal() +
    theme(axis.text=element_text(size=14, color='black'),
          axis.title.x=element_text(size=14, color='black'),
          axis.title.y=element_text(size=14, color='black', hjust=1),
          strip.text=element_text(size=9),
          legend.title=element_text(size=14),
          legend.text=element_text(size=14),
          legend.position='bottom',
          plot.margin=unit(c(0.1, 0.3, 0, 0.1), 'in'),
          panel.border=element_rect(color='black', fill=NA))   
}
